package com.jhgsys.internal.incentive.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.incentive.entity.IncentiveModuleConfig;
import com.jhgsys.internal.incentive.service.IIncentiveModuleConfigService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 *  Controller
 *
 * @author 
 * @date 2024-09-10 16:29:59
 */
@Slf4j
@Validated
@Controller
public class IncentiveModuleConfigController extends BaseController {

    @Autowired
    private IIncentiveModuleConfigService incentiveModuleConfigService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveModuleConfig")
    @RequiresPermissions("incentiveModuleConfig:view")
    public String incentiveModuleConfigIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        if(StringUtils.isNotBlank(getCurrentTenantId())){
            IncentiveModuleConfig incentiveModuleConfig = incentiveModuleConfigService.findIncentiveModuleConfigByTenantId(getCurrentTenantId());
            if(incentiveModuleConfig!=null){
                model.addAttribute("incentiveModuleConfig",incentiveModuleConfig);
            }
        }
        return FebsUtil.view("incentive/incentiveModuleConfig/incentiveModuleConfigList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveModuleConfig/add")
    @RequiresPermissions("incentiveModuleConfig:add")
    public String incentiveModuleConfigAdd(){
        return FebsUtil.view("incentive/incentiveModuleConfig/incentiveModuleConfigEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveModuleConfig/update/{id}")
    @RequiresPermissions("incentiveModuleConfig:edit")
    public String incentiveModuleConfigUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("incentiveModuleConfig", incentiveModuleConfigService.findIncentiveModuleConfigById(id));
        return FebsUtil.view("incentive/incentiveModuleConfig/incentiveModuleConfigEdit");
    }

    /**
     * 查询（单个）
     * @param model
     * @param id
     * @return
     */
    @GetMapping("incentiveModuleConfig/search/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveModuleConfig:view")
    public FebsResponse getIncentiveModuleConfigById(Model model, @PathVariable("id") String  id) {
        IncentiveModuleConfig incentiveModuleConfig = incentiveModuleConfigService.findIncentiveModuleConfigByTenantId(id);
        model.addAttribute("incentiveModuleConfig", incentiveModuleConfig);
        return new FebsResponse().success().data(model);
    }

    /**
     * 查询（所有）
     * @author yangjie
     * @date 2024-09-10 16:29:59
     */
    @GetMapping("incentiveModuleConfig")
    @ResponseBody
    @RequiresPermissions("incentiveModuleConfig:view")
    public FebsResponse getAllIncentiveModuleConfigs(IncentiveModuleConfig incentiveModuleConfig) {
        return new FebsResponse().success().data(incentiveModuleConfigService.findIncentiveModuleConfigList(incentiveModuleConfig));
    }

    /**
     * 查询（分页）
     * @author yangjie
     * @date 2024-09-10 16:29:59
     */
    @GetMapping("incentiveModuleConfig/list")
    @ResponseBody
    @RequiresPermissions("incentiveModuleConfig:view")
    public FebsResponse incentiveModuleConfigList(QueryRequest request, IncentiveModuleConfig incentiveModuleConfig) {
        Map<String, Object> dataTable = getDataTable(this.incentiveModuleConfigService.findIncentiveModuleConfigPage(request, incentiveModuleConfig));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 IncentiveModuleConfig
     * @author yangjie
     * @date 2024-09-10 16:29:59
     */
    @Log("新增IncentiveModuleConfig")
    @PostMapping("incentiveModuleConfig")
    @ResponseBody
    @RequiresPermissions("incentiveModuleConfig:edit")
    public FebsResponse addIncentiveModuleConfig(@RequestBody IncentiveModuleConfig incentiveModuleConfig) throws FebsException {
        try {
            incentiveModuleConfig.setCreatedBy(getCurrentUser().getUserName());
            this.incentiveModuleConfigService.createIncentiveModuleConfig(incentiveModuleConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增IncentiveModuleConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 IncentiveModuleConfig
     * @author yangjie
     * @date 2024-09-10 16:29:59
     */
    @Log("逻辑删除IncentiveModuleConfig")
    @GetMapping("incentiveModuleConfig/delete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveModuleConfig:delete")
    public FebsResponse deleteIncentiveModuleConfig(@PathVariable("id") String incentiveModuleConfigIds) throws FebsException {
        try {
            IncentiveModuleConfig incentiveModuleConfig = new IncentiveModuleConfig();
            incentiveModuleConfig.setDeletedBy(getCurrentUser().getUserName());
            incentiveModuleConfig.setDeletedTime(new Date());
            incentiveModuleConfig.setDeletedFlg(FebsConstant.INVALID);
            this.incentiveModuleConfigService.deleteIncentiveModuleConfig(incentiveModuleConfig,incentiveModuleConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveModuleConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 IncentiveModuleConfig
    * @author yangjie
    * @date 2024-09-10 16:29:59
    */
    @Log("物理删除IncentiveModuleConfig")
    @GetMapping("incentiveModuleConfig/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveModuleConfig:delete")
    public FebsResponse physicallyDeleteIncentiveModuleConfig(@PathVariable("id") String incentiveModuleConfigIds) throws FebsException {
        try {
            this.incentiveModuleConfigService.physicallyDeleteIncentiveModuleConfig(incentiveModuleConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveModuleConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 IncentiveModuleConfig
     * @author yangjie
     * @date 2024-09-10 16:29:59
     */
    @Log("修改IncentiveModuleConfig")
    @PostMapping("incentiveModuleConfig/update")
    @ResponseBody
    @RequiresPermissions("incentiveModuleConfig:edit")
    public FebsResponse updateIncentiveModuleConfig(@RequestBody IncentiveModuleConfig incentiveModuleConfig) throws FebsException {
        try {
            incentiveModuleConfig.setUpdatedBy(getCurrentUser().getUserName());
            this.incentiveModuleConfigService.updateIncentiveModuleConfig(incentiveModuleConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改IncentiveModuleConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
